package mx.com.eit.Testing;

import java.util.List;
import java.util.ArrayList;
import java.util.Random;

import mx.com.eit.Testing.TestThread;

public class TMain {
	
	private static int TLength = 100;
	private static List<TestThread> TArray = new ArrayList<TestThread>(); 
	
	public static void main(String[] args) {
		Random rand;
		int max = 150;
		int min = 35;
		double mem = getMainMemory();
		System.out.println("Initial Memory:" + mem / (1024*1));
		for (int i = 1; i<=TLength; i++){
			rand = new Random();
			int x = rand.nextInt(max-min+1)+min;
			TestThread tt = new TestThread(i,x);
			tt.start();
			TArray.add(tt);
		}
		int working = 0;
		do{
			working = 0;
			for (TestThread thread : TArray){
				if (thread.isAlive()){
					working++;
				}else if (!thread.TPrinted){
					thread.TPrinted = true;
					System.gc();
					mem = getMainMemory();
					System.out.println("Current Memory: "+mem/1024);
				}
			}
		} while (working > 0);
		System.gc();
		mem = getMainMemory();
		System.out.println("After Thread Memory:" + mem / (1024*1));
	}
	
	public static long getMainMemory(){
		return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
	}

}
